package com.hyj.shop.project.login.controller;

import com.hyj.shop.project.user.domain.User;
import com.hyj.shop.project.utils.domain.AjaxResult;
import com.hyj.shop.project.login.domain.Login;
import com.hyj.shop.project.login.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.time.Duration;
import java.util.UUID;

@Controller
@RequestMapping("/api")
public class LoginController {
    @Autowired
    private LoginService loginService;
    @Resource RedisTemplate<String,Object> redisTemplate;
    @RequestMapping(value = "/login")
    @ResponseBody
    public AjaxResult login(@RequestBody Login login){
        System.out.println(login);
        String token= UUID.randomUUID()+"";
        redisTemplate.opsForValue().set(token,"8888888S",Duration.ofMinutes(30L));
        return loginService.login(login);
    }
    @RequestMapping(value = "/loginOut")
    @ResponseBody
    //退出登录
    public AjaxResult loginOut(){
       AjaxResult ajaxResult=AjaxResult.success("操作成功");

      ajaxResult.put("code",200);
        return ajaxResult;
    }
    @RequestMapping(value = "/register")
    @ResponseBody
    //注册
    public AjaxResult Register(@RequestBody User user){
        System.out.println(user);
        return loginService.register(user);
    }
}
